import router from "~/router";
import { getToken } from "~/composables/auth";
import { toast,hideFullLoading,showFullLoading } from "~/composables/util";
import store from "./store"
// 全局前置守卫
router.beforeEach(async (to, from, next) => {
  // 显示loading
  showFullLoading()
  const token = getToken();
  // 没有登录强制返回登录页
  if (!token && to.path != "/Login") {
    toast("请先登录", "error");
    return next({ path: "/Login" });
  }
  //防止重复登录
  if (token && to.path == "/Login") {
    toast("请勿重复登录", "error");
    return next({ path: from.path ? from.path : "/" });
  }
  // 如果用户登录了，自动获取用户信息 存储在vuex中
  if (token) {
    await store.dispatch('getUserInfo')
  }
  let title = to.meta.title?to.meta.title:''
  document.title = title
  // 设置页面标题
  console.log(to.meta.title,'==');
  next();
});
// 全局后置守卫
router.afterEach((to, from) => {
  hideFullLoading()
})
